#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :middleware.py
# @Time      :2025/2/21 09:30
# @Author    :kk
from app01.models import User


# 设置用户信息中间件
def set_user_info(handle):
    def inner(request):
        print("开始设置用户信息中间件------------------------")
        user_id = request.session.get("user_id")
        if user_id:
            print( f"获取用户ID:{user_id}")
            request.user = User.objects.filter(id=user_id).first()
        else :
            print(f"未获取用户ID")
            request.user = None
        print("结束设置用户信息中间件------------------------")
        response = handle(request)

        return response
    return inner
